package JUC.ReLock;

import org.junit.Test;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import static MyUtils.MHUtils.*;
import static MyUtils.MHMoban.*;

public class 用下reentralock {

    static int x = 0; // stat是开启可见性 v是多线可见性
    public static void main(String[] args) throws Exception{
        final Lock lock = new ReentrantLock(); // 匿名 final
        Thread t1 = new Thread(() -> {
            lock.lock();
            try {
                for (int i = 0; i < 3000000; i++) {
                    x++;
                }
            }finally {
                lock.unlock();
            }
        });
        t1.start();
        Thread t2 = new Thread(() -> {
            lock.lock();
            try {
                for (int i = 0; i < 3000000; i++) {
                    x++;
                }
            }finally {
                lock.unlock();
            }
        });
        t2.start();

        t1.join();t2.join();
        System.out.println(x);
    }

}
